<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

abstract class m_backoffice_extends_FormModal extends f_core_Action
{
    public function getContextWebsite()
    {
        $doc = $this->getDocumentFromRequest();
        if($doc->getModel() == 'modules_website/website')
        {
            return $doc;
        }
        
        $parents = f_relation_Manager::getInstance()->getParentsByDocType($doc->getUniqueId(), 'modules_website/website', $doc->getLang());
        //$parents = f_relation_ManagerNew::getInstance($doc)->getParents()->withModel('website/website')->recursively()->withLang($doc->getLang())->retrieveDocuments();
        return $parents[0];
    }

    public function isFormSubmit()
    {
        return (count($this->getFormValues('form')) > 0);
    }

    /**
     * Get edited document from request
     * MUST BE DEFINED WITH docId AND docLang !!!
     *
     * @return f_document_BaseDocument
     */
    public function getDocumentFromRequest()
    {
        $tempDocService = m_backoffice_services_TempDocService::getInstance();
        return $tempDocService->getDocument($this->getRequestParameter('docId'), $this->getRequestParameter('docLang'));
    }

    /**
     * Set parameters for modal template
     */
    public function setModalParameters()
    {
        $parameters = $this->getRequest()->getParameters();

        if(isset($parameters['href']) && is_array($parameters['href']))
        {
            $parameters['href'] = '?module=backoffice&action=AjaxContext&href='.$parameters['href'][0];
        }
        else
        {
            $parameters['href'] = $_SERVER['REQUEST_URI'];
        }

        $parameters['formValue'] = $this->getFormValues('form');

        $this->addToModel('modal', $parameters);
    }

    public function closeModalAndRefresh()
    {
        die('<div id="formsuccess" style="display:none">refreshAll</div>');
    }

    public function closeModalAndRefreshMenu()
    {
        die('<div id="formsuccess" style="display:none">reloadMenu</div>');
    }

    /**
     * Close modal and refresh page edition
     */
    public function closeModalAndRedirect($url)
    {
        die('<div id="formsuccess" style="display:none">'.$url.'</div>');
    }

    /**
     * Default validate action
     * @return boolean
     */
    public function validate()
    {
        return $this->getUserContext()->hasRight("backoffice_accessBo");
    }

    /**
     * Default errorHandler
     * @return string
     */
    public function errorHandler()
    {
        return f_template_Template::getInstance()->setModuleTemplate('backoffice', 'block/modal/ModalNoRights.html')->render();
    }
}